##Intragroup_Conflict_Leadership_Appendix_Intelligence.R
##Appendix study of intelligence sharing by Trump, GOP respondents

##set working directory
setwd("C:/Users/lmh735/Dropbox (Political Science)/Partisanship and leadership-Trump/2021 POBE Submission/R&R/Replication Files")

##load data
data2.rep<- read.csv("Appendix_Study_Intelligence_Sharing.csv", header=TRUE)

##Output Directory
setwd("C:/Users/lmh735/Dropbox (Political Science)/Partisanship and leadership-Trump/2021 POBE Submission/R&R/Replication Files/Output")

###################################
##Appendix Table G2
##Balance using chi-squared
library(MASS) ##(only run for this, causes issues with tdyr/dplyr, need to detach if)
##gender
balance.gender<- table(data2.rep$female, data2.rep$treatment.rebuke.labels)
prop.table(balance.gender, 2)
chisq.test(balance.gender)
##education 
balance.educ<- table(data2.rep$educ, data2.rep$treatment.rebuke.labels)
prop.table(balance.educ, 2)
chisq.test(balance.educ)
##income
balance.income<- table(data2.rep$income, data2.rep$treatment.rebuke.labels)
prop.table(balance.income, 2)
chisq.test(balance.income)
##age (categorical)
balance.age.cat<- table(data2.rep$age.cat, data2.rep$treatment.rebuke.labels)
prop.table(balance.age.cat, 2)
chisq.test(balance.age.cat)

##sample demographics
##gender
sample.gender<- table(data2.rep$female)
prop.table(sample.gender)
##education 
sample.educ<- table(data2.rep$educ)
prop.table(sample.educ)
##income
sample.income<- table(data2.rep$income)
prop.table(sample.income)
##age (categorical)
sample.age.cat<- table(data2.rep$age.cat)
prop.table(sample.age.cat)

detach("package:MASS", unload=TRUE)
detach("package:psych", unload=TRUE)

###################################
##Appendix Table G3 
##summary stats
##approval of Trump
summary(data2.rep$approve.trump.01)
length(na.omit(data2.rep$approve.trump.01))
sd(data2.rep$approve.trump.01, na.rm=TRUE)
##vote for Trump 
summary(data2.rep$trump.vote.01)
length(na.omit(data2.rep$trump.vote.01))
sd(data2.rep$trump.vote.01, na.rm=TRUE) 
##Agreement with Republican response
summary(data2.rep$agree.rep.response.01)
length(na.omit(data2.rep$agree.rep.response.01))
sd(data2.rep$agree.rep.response.01, na.rm=TRUE)
##vote for Congress GOP (not sure and Dem pooled in 0)
summary(data2.rep$vote.cong.gop)
length(na.omit(data2.rep$response.app.01))
sd(data2.rep$vote.cong.gop, na.rm=TRUE) 
##appropriateness of GOP response
summary(data2.rep$response.app.01)
length(na.omit(data2.rep$response.app.01))
sd(data2.rep$response.app.01, na.rm=TRUE)
##primary vote if Senator signed leadership letter
summary(data2.rep$vote.primary.01)
length(na.omit(data2.rep$vote.primary.01))
sd(data2.rep$vote.primary.01, na.rm=TRUE) 


###############################
#Analysis with dplyr
library(dplyr)
library(tidyr)
library(infer)
library(ggplot2)
library(gridExtra)

##N by condition
##Appendix Table G1
intell.N<- data2.rep %>%
  mutate(ResponseCondition.new=factor(treatment.rebuke.labels, levels=c("Loyalist", "Dissent"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(ResponseCondition.new, approve.trump.01) %>%
  gather("measure", "value", -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  group_by(ResponseCondition.new) %>%
  summarize(n=n())
intell.N
sum(intell.N$n)

###############################
##Appendix G5: t-test results
##dissent is 1, loyalty is 0 in treatment

##approval of Trump
t.test(as.numeric(data2.rep$approve.trump.01) ~ data2.rep$treatment.rebuke, na.rm=T, alternative="two.sided")

##vote for Trump 
t.test(as.numeric(data2.rep$trump.vote.01) ~ data2.rep$treatment.rebuke, na.rm=T, alternative="two.sided")

##appropriateness of GOP response
t.test(data2.rep$response.app.01 ~ data2.rep$treatment.rebuke, na.rm=T, alternative="two.sided")

##Agreement with Republican response
t.test(data2.rep$agree.rep.response.01 ~ data2.rep$treatment.rebuke, na.rm=T, alternative="two.sided")

##vote for Congress GOP (not sure and Dem pooled in 0)
t.test(as.numeric(data2.rep$vote.cong.gop) ~ data2.rep$treatment.rebuke, na.rm=T, alternative="two.sided")

##primary vote if Senator signed leadership letter
t.test(as.numeric(data2.rep$vote.primary.01) ~ data2.rep$treatment.rebuke, na.rm=T, alternative="two.sided")

##interaction with pre-treatment Trump FT
vote.primary.interaction<- lm(vote.primary.01 ~ treatment.rebuke*trump_ft_pre, data=data2.rep)
summary(vote.primary.interaction) 


######################################
##Comparison of Republicans to 2016 ANES
summary(data2.rep$trump_ft_pre)
sd(data2.rep$trump_ft_pre)

summary(data2.rep$gop_ft_pre)
sd(data2.rep$gop_ft_pre)
